<?php
/**
 * 第三方地图服务
 * panglishan
 * 2021.01.14
 */
namespace App\Services\Tools;

use App\Exceptions\BasicException;
use App\Services\Tools\CurlService;

class MapAreaService
{

    /*****************************************百度地图服务*********************************/

    const AK = 'Qrtl2bnweFNgmEKTlp2ImaDitR9VGl7N';
    const SK = 'Iabpmml2lxvdk4pv8gp5kW0LqXUrGSCq';
    const PLACE_SEARCH_URL = 'http://api.map.baidu.com/place/v2/search';
    const PLACE_SEARCH_URI = '/place/v2/search';

    const PLACE_SUGGESTION_URL = 'http://api.map.baidu.com/place/v2/suggestion';
    const PLACE_SUGGESTION_URI = '/place/v2/suggestion';

    /**
     * 获取SN
     * @param $ak
     * @param $sk
     * @param $url
     * @param $querystring_arrays
     * @param string $method
     * @return string
     */
    static function caculateAKSN($ak, $sk, $url, $querystring_arrays, $method = 'GET')
    {
        if ($method === 'POST'){
            ksort($querystring_arrays);
        }
        $querystring = http_build_query($querystring_arrays);
        return md5(urlencode($url.'?'.$querystring.$sk));
    }


    /**
     * 根据经纬度获取周边地理位置
     * @param $query
     * @param $lat
     * @param $lng
     * @return array
     * @throws BasicException
     */
    static function placeSearch($query,$lat,$lng)
    {

        $data = [
            'query' => $query,
            'location' => $lat.','.$lng,
            'output' => 'json',
            'ak' => self::AK,
            'radius' => 2000,
            'page_size' => 20,
            'filter' => 'sort_name:distance|sort_rule:1',
        ];

        $data['sn'] = self::caculateAKSN(self::AK,self::SK,self::PLACE_SEARCH_URI,$data);

        $res = CurlService::curl_get(self::PLACE_SEARCH_URL,$data);

        $result = json_decode(json_encode($res),true);

        if(isset($result['status']) && ($result['status'] == 0))
        {
            $arr = [];
            array_walk($result['results'],function($value, $key) use (&$arr){
                if(isset($value['location'])) {
                    $arr[] = [
                        'name' => $value['name'],
                        'location' => $value['location']
                    ];
                }
            });

//            return array_column($result['results'],'name');
            return $arr;

        } else {
            return [];
        }
    }


    /**
     * 根据关键字推荐地理位置
     * @param $query
     * @param $location
     * @return array
     * @throws BasicException
     */
    static function placeSuggestion($query,$location)
    {
        $data = [
            'query' => $query,
            'region' => '中国',
            'location' => $location,
            'output' => 'json',
            'ak' => self::AK
        ];

        $data['sn'] = self::caculateAKSN(self::AK,self::SK,self::PLACE_SUGGESTION_URI,$data);

        $res = CurlService::curl_get(self::PLACE_SUGGESTION_URL,$data);

        $result = json_decode(json_encode($res),true);

        if(isset($result['status']) && ($result['status'] == 0))
        {
            $arr = [];
            array_walk($result['result'],function($value, $key) use (&$arr){
                if(isset($value['location'])) {
                    $arr[] = [
                        'name' => $value['name'],
                        'location' => $value['location']
                    ];
                }
            });

            return $arr;

        } else {
            return [];
        }


    }


}